package com.teacher.hong.dao;

import com.teacher.hong.entity.User;
import org.springframework.stereotype.Repository;
import sun.swing.UIAction;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * 用户类数据操作
 */
@Repository
public class UserDao {

    @Resource
    private BaseDao baseDao;

//    public UserDao(BaseDao baseDao) {
//        this.baseDao = baseDao;
//        System.out.println("实例UserDao了...");
//    }

//    public void setBaseDao(BaseDao baseDao) {
//        this.baseDao = baseDao;
//    }

    /**
     * 根据用户账号、密码查询用户对象
     * @param account
     * @return
     * @throws SQLException
     */
    public User selectByAccount(String account) throws SQLException {
        User user = null;
        ResultSet resultSet = baseDao.query("select * from user where account=?",
                Arrays.asList(account));
        if (resultSet.next()) {
            user = new User();
            user.setAccount(account);
            user.setPassword(resultSet.getString("password"));
        }
        return user;
    }

    /**
     * 根据用户账号、密码查询条数
     * @param user
     * @return
     * @throws SQLException
     */
    public int selectByUser(User user) throws SQLException {
        int count = 0;
        //方法1，创建一个集合，然后通过add方法添加参数
//            List<Object> params = new ArrayList<>();
//            params.add(user.getAccount());
//            params.add(user.getPassword());

        //方法2，通过数组转换成一个List集合
//            Object[] objects = {user.getAccount(), user.getPassword()};
//            List<Object> list = Arrays.asList(objects);

        ResultSet resultSet = baseDao.query("select count(0) from user where account=? and password=?",
                Arrays.asList(user.getAccount(), user.getPassword()));
        if (resultSet.next()) {
            count = resultSet.getInt(1);
        }
        return count;
    }

}
